<?php

class m110923_075351_receipt extends CDbMigration {
	public function safeUp() {
		$this
			->createTable('erp_material_receipt',
				array(
					'id' => 'pk',
					'projectId' => 'int',
					'receiptDate' => 'date',
					'merchantId' => 'int',
					'handler' => 'string',
					'note' => 'string',
					'type'=>'int',
					'processingCost'=>'decimal(16,5)',
					'transportationCost'=>'decimal(16,5)',
					'discount'=>'decimal(16,5)',
					'otherCost'=>'decimal(16,5)',
					'warehouseManagerId' => 'int',
					'createTime' => 'datetime',
					'createUserId' => 'int',
					'updateTime' => 'datetime',
					'updateUserId' => 'int',
				));
		$this
			->createIndex("material_receipt_create_user_id", 'erp_material_receipt', 'createUserId');
		$this
			->addForeignKey('fk_material_receipt_create_user_id', 'erp_material_receipt',
				'createUserId', 'erp_user', 'id', 'SET NULL', 'CASCADE');
		$this
			->createIndex("material_receipt_update_user_id", 'erp_material_receipt', 'updateUserId');
		$this
			->addForeignKey('fk_material_receipt_update_user_id', 'erp_material_receipt',
				'updateUserId', 'erp_user', 'id', 'SET NULL', 'CASCADE');
		$this->createIndex("material_receipt_project_id", 'erp_material_receipt', 'projectId');
		$this
			->addForeignKey('fk_material_receipt_project_id', 'erp_material_receipt', 'projectId',
				'erp_project', 'id', 'SET NULL', 'CASCADE');
		$this->createIndex("material_receipt_merchant_id", 'erp_material_receipt', 'merchantId');
		$this
			->addForeignKey('fk_material_receipt_merchant_id', 'erp_material_receipt',
				'merchantId', 'erp_merchant', 'id', 'SET NULL', 'CASCADE');
		$this
			->createIndex("material_receipt_warehouse_manager_id", 'erp_material_receipt',
				'warehouseManagerId');
		$this
			->addForeignKey('fk_material_receipt_warehouse_manager_id', 'erp_material_receipt',
				'warehouseManagerId', 'erp_employee', 'id', 'SET NULL', 'CASCADE');

		$this
			->createTable('erp_material_receipt_item',
				array(
					'id' => 'pk',
					'materialReceiptId' => 'int',
					'materialModelId' => 'int',
					'brand' => 'string',
					'amount' => 'decimal(16,5)',
					'unit' => 'string',
					'amountStandardUnit' => 'decimal(16,5)',
					'unitPrice' => 'decimal(16,5)',
					'standardUnitPrice' => 'decimal(16,5)',
					'totalPrice'=>'decimal(16,5)',
					'note' => 'string',
					'createTime' => 'datetime',
					'createUserId' => 'int',
					'updateTime' => 'datetime',
					'updateUserId' => 'int',
				));

		$this
			->createIndex("material_receipt_item_create_user_id", 'erp_material_receipt_item',
				'createUserId');
		$this
			->addForeignKey('fk_material_receipt_item_create_user_id', 'erp_material_receipt_item',
				'createUserId', 'erp_user', 'id', 'SET NULL', 'CASCADE');
		$this
			->createIndex("material_receipt_item_update_user_id", 'erp_material_receipt_item',
				'updateUserId');
		$this
			->addForeignKey('fk_material_receipt_item_update_user_id', 'erp_material_receipt_item',
				'updateUserId', 'erp_user', 'id', 'SET NULL', 'CASCADE');

		$this
			->createIndex("material_receipt_item_material_id", 'erp_material_receipt_item',
				'materialModelId');
		$this
			->addForeignKey('fk_material_receipt_item_material_id', 'erp_material_receipt_item',
				'materialModelId', 'erp_material_model', 'id', 'SET NULL', 'CASCADE');

		$this
			->createIndex("material_material_receipt_item_material_receipt_id",
				'erp_material_receipt_item', 'materialReceiptId');
		$this
			->addForeignKey('fk_mmaterial_receipt_item_material_receipt_id',
				'erp_material_receipt_item', 'materialReceiptId', 'erp_material_receipt', 'id',
				'CASCADE', 'CASCADE');
		
	}

	public function safeDown() {
		$this->dropTable('erp_material_receipt_item');
		$this->dropTable('erp_material_receipt');
	}
}
